package com.mer.sevice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mer.dao.SpecialDao;
import com.mer.entity.Special;
import com.mer.sevice.SpecialService;
import com.mer.vo.CommonResult;
import com.mer.vo.SpecialVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

/**
 * @program: SpecialServiceImpl
 * @description:
 * @author: 吴昊
 * @create: 2021/10/19 9:37
 **/
@Service
public class SpecialServiceImpl implements SpecialService {

    @Resource
    private SpecialDao specialDao;

    @Override
    public CommonResult getAllSpecial(Integer page, Integer limit, SpecialVo specialVo) {

        QueryWrapper<Special> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(specialVo.getZoneName())) {
            wrapper.like("zone_name", specialVo.getZoneName());
        }
        if (specialVo.getIsDisable() != null) {
            wrapper.eq("is_disable", specialVo.getIsDisable()) ;
            }
        Page<Special> page1 = new Page<>(page, limit);
        Page<Special> specialPage = specialDao.selectPage(page1, wrapper);
        if (specialPage.getSize()>0){
             return new CommonResult(2000,"查询成功",specialPage);
        }else {
            return new CommonResult(5000,"查询失败",null);
        }



    }

    @Override
    public CommonResult exchange(String id, Integer isDisable) {
        Special special = new Special();
        special.setId(id);
        special.setIsDisable(isDisable);
        int i = specialDao.updateById(special);
        if (i>0){
            return new CommonResult(2000,"修改状态成功",null);
        }else {
            return new CommonResult(5000,"修改状态失败",null);
        }

    }

    @Override
    public CommonResult add(SpecialVo specialVo) {
        Special special = new Special();

        if (StringUtils.isNotEmpty(specialVo.getId())){
            special.setZoneName(specialVo.getZoneName());
            special.setZoneDesc(specialVo.getZoneDesc());
            special.setId(specialVo.getId());
            int i = specialDao.updateById(special);
            if (i>0){
                return  new CommonResult(2000,"修改成功",null);
            }else {
                return new CommonResult(5000,"修改失败",null);
            }
        }else {
            special.setZoneName(specialVo.getZoneName());
            special.setZoneDesc(specialVo.getZoneDesc());
            special.setId(specialVo.getId());
            int insert = specialDao.insert(special);
            if (insert>0){
                return new CommonResult(2000,"添加成功",null);
            }else {
                return new CommonResult(5000,"添加失败",null);
            }
        }


    }

    @Override
    public CommonResult delete(SpecialVo specialVo) {
        Special special = new Special();
        special.setId(specialVo.getId());
        special.setIsDeleted(1);
        int i = specialDao.deleteById(special);
        if (i > 0) {
            return new CommonResult(2000,"删除成功",null);
        }else {
            return new CommonResult(5000,"删除失败",null);
        }



    }


}
